package cn.com.hfuu;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 自定义拦截器
 * @author git
 *
 */
public class LoginInterceptor implements HandlerInterceptor{
	
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object object) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("-----------------自定义拦截器---------------");
		if(request.getRequestURI().indexOf("login/home")>=0||request.getRequestURI().indexOf("login/dologin")>=0||request.getRequestURI().indexOf("login/code")>=0||request.getRequestURI().indexOf("login/checkcode")>=0||request.getRequestURI().indexOf("qr")>=0){
			return true;
		} 
		if(request.getSession().getAttribute("user")!=null){
			return true;
		}
		 PrintWriter out = response.getWriter();  
	        out.println("<html>");      
	        out.println("<script>");      
	        out.println("window.parent.location.href='"+request.getContextPath()+"/login/home')");      
	        out.println("</script>");      
	        out.println("</html>");
		response.sendRedirect("/TeaSystem/login/home"); 
			return false;
		
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object object, ModelAndView m) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object object, Exception e)
			throws Exception {
		// TODO Auto-generated method stub
		
	}



	

}
